package com.yt.user.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.user.models.service.IPhoneInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

import com.yt.user.models.bo.PhoneInfoSaveBO;
import com.yt.user.models.bo.PhoneInfoBO;
import com.yt.user.models.dto.PhoneInfoDTO;

/**
 * 用户信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:48:45
 */

@RestController
@RequestMapping("/cms/userInfo")
@Slf4j
@Api(value = "UserInfoController",tags = "用户信息接口")
public class PhoneInfoController {

    @DubboReference
    private IPhoneInfoService PhoneInfoService;

    @ApiOperation(value = "添加用户信息",notes = "添加用户信息")
    @PostMapping(value = "/insert")
    public Result insert(@RequestBody PhoneInfoSaveBO saveBO) {
        // 参数验证
        if (saveBO == null || saveBO.getPhone() == null || saveBO.getPhone().isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        boolean flag = PhoneInfoService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存用户信息",notes = "保存用户信息")
    public Result save(@RequestBody PhoneInfoSaveBO saveBO){
        // 参数验证
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        if(saveBO.getPhone() == null || saveBO.getPhone().isEmpty()){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改用户信息",notes = "修改用户信息")
    @PostMapping("/update")
    public Result update(@RequestBody PhoneInfoSaveBO saveBO){
        log.info("接收到更新请求: {}", saveBO);
        
        // 检查参数
        if (saveBO == null) {
            log.warn("请求参数saveBO为空");
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        if (saveBO.getPhone() == null || saveBO.getPhone().isEmpty()) {
            log.warn("请求参数中phone字段为空: {}", saveBO.getPhone());
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        PhoneInfoDTO dto = new PhoneInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        log.info("准备更新的DTO对象: {}", dto);
        
        boolean flag = PhoneInfoService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            log.warn("更新失败，手机号: {}", saveBO.getPhone());
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "String"),
             @ApiImplicitParam(name = "status",value = "号码状态（0-停用，1-正常，2-黑名单）",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(String id, Integer status){
         PhoneInfoDTO dto = new PhoneInfoDTO();
         dto.setPhone(id); // 直接使用String类型的phone作为主键
         dto.setStatus(status);
        boolean flag = PhoneInfoService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除用户信息",notes = "删除用户信息")
    @ApiImplicitParam(name = "id",value = "用户信息id",required = true,paramType = "query",dataType = "String")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") String id){
        boolean flag = PhoneInfoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除用户信息",notes = "批量删除用户信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        PhoneInfoService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "String")
    public Result<PhoneInfoDTO> get(@RequestParam("id") String id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        PhoneInfoDTO dto = PhoneInfoService.selectById(id);
        // 添加调试信息
        if (dto != null) {
            log.info("查询到的用户信息: phone={}, createTime={}, updateTime={}", 
                     dto.getPhone(), dto.getCreateTime(), dto.getUpdateTime());
        }

        return new Result<>(dto);
    }

    @ApiOperation(value = "用户信息列表",notes = "用户信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<PhoneInfoDTO>> queryPageList(PhoneInfoBO bo){
        Wrapper<PhoneInfoDTO> wrapper = new EntityWrapper<>();

                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<PhoneInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = PhoneInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "用户信息列表",notes = "用户信息列表")
    @PostMapping("/listAll")
    public Result<List<PhoneInfoDTO>> queryList(PhoneInfoBO bo){
        Wrapper<PhoneInfoDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }
        List<PhoneInfoDTO> list = PhoneInfoService.selectList(wrapper);
        return new Result<>(list);
    }
}